package com.tianhua.javs.core.func.translate.varassign;

import com.google.common.collect.Lists;
import com.tianhua.javs.client.bean.JavsSegmentBean;
import com.tianhua.javs.client.bean.ScriptVarMappingBean;
import com.tianhua.javs.client.func.ITranslatorFunc;
import com.tianhua.javs.core.annotations.Segment;
import com.tianhua.javs.core.func.translate.AbstractTranslator;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * Description:
 * date: 2022/2/12
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Component(value = "listfunc")
@Segment(tag = "list",scriptFuncBean = "assignFunc")
public class ListFunc extends AbstractTranslator implements ITranslatorFunc {
    @Override
    public JavsSegmentBean translate(String shellSegment) {
        if(shellSegment.trim().startsWith("list(")){
            shellSegment = shellSegment.replaceFirst("list","Lists.newArrayList");
            JavsSegmentBean javsSegmentBean =  JavsSegmentBean.getMatchInstance(shellSegment,"List","com.google.common.collect.Lists","java.util.List");
            ScriptVarMappingBean scriptVarMappingBean = ScriptVarMappingBean.builder().varClassName("List").referClassName("List").build();
            javsSegmentBean.setScriptVarMappingBean(scriptVarMappingBean);

            return javsSegmentBean;
        }
        if(shellSegment.trim().startsWith("list<")){
            shellSegment = shellSegment.replaceFirst("list","new ArrayList");
            String segmentClass = getGenerateSegment(shellSegment,"List");

            JavsSegmentBean javsSegmentBean = JavsSegmentBean.getMatchInstance(shellSegment,segmentClass,"java.util.List","java.util.ArrayList");
            ScriptVarMappingBean scriptVarMappingBean = ScriptVarMappingBean.builder().varClassName("List").referClassName("List").build();
            scriptVarMappingBean.setVarGenerateClassType(Lists.newArrayList(segmentClass));
            javsSegmentBean.setScriptVarMappingBean(scriptVarMappingBean);

            return javsSegmentBean;
        }
        if(shellSegment.trim().startsWith("linkedlist(")){
            shellSegment = shellSegment.replaceFirst("list","Lists.newLinkedList");
            JavsSegmentBean javsSegmentBean = JavsSegmentBean.getMatchInstance(shellSegment,"List","com.google.common.collect.Lists","java.util.List");
            ScriptVarMappingBean scriptVarMappingBean = ScriptVarMappingBean.builder().varClassName("List").referClassName("List").build();
            javsSegmentBean.setScriptVarMappingBean(scriptVarMappingBean);
            return javsSegmentBean;
        }
        if(shellSegment.trim().startsWith("linkedlist<")){
            shellSegment = shellSegment.replaceFirst("list","new LinkedList");
            String segmentClass = getGenerateSegment(shellSegment,"List");

            JavsSegmentBean javsSegmentBean = JavsSegmentBean.getMatchInstance(shellSegment,segmentClass,"java.util.LinkedList","java.util.List");

            ScriptVarMappingBean scriptVarMappingBean = ScriptVarMappingBean.builder().varClassName("List").referClassName("List").build();
            scriptVarMappingBean.setVarGenerateClassType(Lists.newArrayList(segmentClass));
            javsSegmentBean.setScriptVarMappingBean(scriptVarMappingBean);
            return javsSegmentBean;
        }
        return JavsSegmentBean.getInstance(shellSegment);
    }
}
